interface useTauriRequestOptions {
  headers?: {};
  responseType?: "JSON" | "STREAM" | "TEXT";
  method?: "GET" | "POST";
  // ...其他选项
}

export const useTauriRequest = async (
  url: string,
  data: any = {},
  options: useTauriRequestOptions = {
    headers: {},
    responseType: "JSON",
    method: "GET",
  },
  handler?: (data: string) => void
) => {
  let unListen = null;
  if (options.responseType === "STREAM") {
    unListen = await appListen("event_source", (data) => {
      handler!(data.payload as string);
    });
  }
  const response: Response = await appInvoke("http_request", {
    url,
    data: JSON.stringify(data),
    options: {
      headers: options.headers ?? {},
      method: options.method ?? "GET",
      response_type: options.responseType,
    },
  });
  return {
    unListen,
    response,
  };
};
